

package com.maoshi.shop.platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.maoshi.shop.bean.enums.ShopWalletChangeReason;
import com.maoshi.shop.bean.model.ShopDetail;
import com.maoshi.shop.bean.model.ShopWithdrawCash;
import com.maoshi.shop.common.annotation.SysLog;
import com.maoshi.shop.common.exception.MaoshiShopBindException;
import com.maoshi.shop.common.util.Arith;
import com.maoshi.shop.common.util.PageParam;
import com.maoshi.shop.security.comment.model.AppConnect;
import com.maoshi.shop.security.comment.service.AppConnectService;
import com.maoshi.shop.security.platform.util.SecurityUtils;
import com.maoshi.shop.service.ShopDetailService;
import com.maoshi.shop.service.ShopWithdrawCashService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * 商家提现申请信息
 *
 * @author LGH
 * @date 2019-09-29 11:10:12
 */
@RestController
@AllArgsConstructor
@RequestMapping("/shop/shopWithdrawCash")
public class ShopWithdrawCashController {

    private final ShopWithdrawCashService shopWithdrawCashService;

    private final AppConnectService appConnectService;

    private final ShopDetailService shopDetailService;


    /**
     * 分页查询
     *
     * @param page             分页对象
     * @param shopWithdrawCash 商家提现申请信息
     * @return 分页数据
     */
    @GetMapping("/page")
    public ResponseEntity<IPage<ShopWithdrawCash>> getShopWithdrawCashPage(PageParam<ShopWithdrawCash> page, ShopWithdrawCash shopWithdrawCash) {
        return ResponseEntity.ok(shopWithdrawCashService.pageShopWithdrawCash(page, shopWithdrawCash));
    }


    /**
     * 通过id查询商家提现申请信息
     *
     * @param cashId id
     * @return 单个数据
     */
    @GetMapping("/info/{cashId}")
    public ResponseEntity<ShopWithdrawCash> getById(@PathVariable("cashId") Long cashId) {
        return ResponseEntity.ok(shopWithdrawCashService.getById(cashId));
    }


    /**
     * 审核信息
     */
    @SysLog("审核商家提现信息")
    @PutMapping("/audit")
    @PreAuthorize("@pms.hasPermission('shop:shopWithdrawCash:audit')")
    public ResponseEntity<Void> audit(@RequestBody ShopWithdrawCash shopWithdrawCash) {
        ShopWithdrawCash dbShopWithdrawCash = shopWithdrawCashService.getById(shopWithdrawCash.getCashId());
        if (dbShopWithdrawCash == null) {
            throw new MaoshiShopBindException("未找到申请信息");
        }
        ShopDetail shopdetail = shopDetailService.getShopDetailByShopId(dbShopWithdrawCash.getShopId());
        if (shopdetail == null) {
            throw new MaoshiShopBindException("未找到该店铺信息");
        }
        AppConnect appConnect = appConnectService.getOne(new LambdaQueryWrapper<AppConnect>()
                .eq(AppConnect::getAppId, 1)
                .eq(AppConnect::getUserId, shopdetail.getUserId()));
        if (appConnect == null) {
            throw new MaoshiShopBindException("未找到该店铺身份信息");
        }
        shopWithdrawCash.setCreateTime(null);

        shopWithdrawCashService.auditWithdrawCash(shopWithdrawCash.getCashId(), shopWithdrawCash, SecurityUtils.getSysUser().getUserId(), appConnect.getBizUserId());
        // 减少冻结金额
        return ResponseEntity.ok().build();
    }
}
